<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html> 
<head>
<%@ include file="../../include/easyui.jsp" %>
<%@ include file="../../include/pForm.jsp" %>
<%@ include file="/WEB-INF/jsp/include/easyui-layui-upload.jsp" %>
<script src="/plug-in/jquery-easyui-1.5.1/plugins/jquery.choosebox.js"></script>
<script type="text/javascript" src="${path}/plug-in/jquery-easyui-1.5.1/plugins/datagrid-cellediting.js"></script>
<title>支出编辑页面</title>
<script>
	$(function(){
		fn.resetForm();
		var _data = fn.getDialogData();
		
		$("#dataGrid").datagrid({
			idField: "id",
			rownumbers: true,
			//pagination: true,
			singleSelect: true, 
			showFooter: true,
			pageSize:500,
			pageList:[500],
			nowrap:false,
			url : '/console/com.fs.jzzn.pojo.ExpenseItem/list',
			queryParams: {
				expenseNo :_data.expenseNo,
				expenseNo_searchType: "eq"
			},
			fitColumns:true,
			columns : [[
			    	 {field: 'costUser', title: '支出对象', width: 80,editor:{
						 type:'combobox',
							options:{
								method:'get',
								url:'/console/combobox?valueField=nickName&textField=nickName&modleName=user',
								required:true
							}
					 }}
			    	 ,{field: 'remark', title: '标题说明', width: 200,editor:{
				    		type: 'textbox',
				    		options:{
				    			required:true,
				    			validType:['length[0,500]']
				    		}
				     }}
				    ,{field: 'costType', title: '类别', width: 80,editor:{
				    	type:'combobox',
						options:{
							method:'get',
							url:'/console/combobox?valueField=dicName&textField=dicName&modleName=dictionary&pCode=B1&pCode_searchType=eq',
							required:true
						}
				    }}
			    	,{field: 'money', title: '金额(元)', width: 80,editor:{type:'numberbox',options:{precision:2,required:true}}}
			    	,{field: 'billsNo', title: '凭单号', width: 100,editor:'textbox'}
			    	,{field: 'pNo', title: '路段编号', width: 90,editor:{
				    	type:'combobox',
						options:{
							method:'get',
							url:'/console/combobox?valueField=pNo&textField=pRoad&modleName=project'
						}
				    }}
			    	,{field: 'department', title: '关联项目部', width: 130,editor:{
				    	type:'combobox',
						options:{
							method:'get',
							editable: false,
							url:'/console/combobox?valueField=orgName&textField=orgName&modleName=org&orgType=施工队&orgType_searchType=neq'
							//url:'/console/combobox?valueField=dicName&textField=dicName&modleName=dictionary&pCode=A5&pCode_searchType=eq'
						}
				    }}
			    	,{field: 'costDate', title: '日期', width: 100,editor:{
				    	type:'datebox'
				    }}
			    	,{field: 'opt', title: '操作', width: 50,formatter: function(value,row,index){
			    		if(row.costType!='合计：'){
				    		return '<a href="#" onclick="deleteRow('+ index +')">删除</a>';
			    		}
			    	}}
			]],
			onLoadSuccess:function(){
				childTotal();
			},
			onAfterEdit: function(index, row, changes){
				$.each(['remark','costType','billNo'], function (i,item) {
					$("td[field="+ item +"] div").tooltip({ content: function(){ return $(this).text(); } });
				});
				var dData = $('#dataGrid').datagrid('getData');
				var total = {money: 0, costType: '合计：'};
				$.each(dData.rows ||[], function (i,item) {
					total.money += parseFloat(item.money || '0');
				});
				total.money = total.money.toFixed(2);
				
				$('#dataGrid').datagrid('reloadFooter',[ total ]);
			},
			onEndEdit: function(index, row, changes){
				$.each(['remark','costType','billNo'], function (i,item) {
					$("td[field="+ item +"] div").tooltip("destroy");
				});
			},
			toolbar: [{
				iconCls: 'icon-add', text: '单行添加', handler: function(){
					 $('#dataGrid').datagrid('appendRow',{money:0});
				}
			},'-'
			,{
				iconCls: 'icon-add', text: '快速导入', id: 'importBut',handler: function(){
				}
			},'-'
			,{
				iconCls: 'icon-ok',text: '完成编辑', handler: function(){
					var row = $("#dataGrid").datagrid("getSelected");
					var index = $("#dataGrid").datagrid("getRowIndex",row);
					$("#dataGrid").datagrid("endEdit",index);
				}
			},'-'
			,{
				iconCls: 'icon-cut',text: '删除', handler: function(){
					var row = $("#dataGrid").datagrid("getSelected");
					var index = $("#dataGrid").datagrid("getRowIndex",row);
					if(row && index>-1){
						$("#dataGrid").datagrid("deleteRow",index);
					}
				}
			},'-'
			,{
				iconCls: 'icon-help',text: '帮助', handler: function(){
					$.messager.show({ title:'温馨提示', msg: '点击添加按钮，添加支出明细!',timeout:3000, showType:'slide' });
				}
			},'-'
			,{
				iconCls: 'icon-arrow-down',text: '下载模板', handler: function(){
					$.messager.show({ title:'温馨提示', msg: '工程支出展开明细，点击【支出明细导出】按钮下载模板!',timeout:3000, showType:'slide' });
				}
			}
			]
		}).datagrid('enableCellEditing');
		
		/*表单提交前动作*/
		fn.onBeforeSubmitForm = function(){
			/*自动完成编辑状态*/
			var rows = $("#dataGrid").datagrid('getRows') ;
			for(var index=0,len=rows.length;index<len;index++){
				$("#dataGrid").datagrid('endEdit',index);
			}
			$("input[name=childJson]").val(JSON.stringify(rows));
		};
		
		setTimeout(function(){
			$("label[for=_easyui_textbox_input3]").css({"line-height":"60px"});
		},500);
	});
	
	layui.use(['upload'], function(){
		  var upload = layui.upload,
		  		$ = layui.jquery;
		  setTimeout(function(){
			  upload.render({ //允许上传的文件后缀
				    elem: '#importBut'
				    ,url: '/console/layui/upload/'
				    ,accept: 'file' //普通文件
				    ,exts: 'xls|xlsx' //只允许上传压缩文件
				    ,done: function(res){
				    	$.getJSON('/console/expense/rewrite/getImportDataGrid', {id: res.data.id} ,function(json){
							if(json.flag){
								$.each(json.data.rows, function (i,item) {
									if(item.remark && item.money){
										$('#dataGrid').datagrid('appendRow',item);
									}
								});
								$("#okBut").click();
							}else{
								alert(json.msg);  
							}
			        	 });
				    }
				  });
		  },2000);
	});
	
	function childTotal(){
		var dData = $('#dataGrid').datagrid('getData');
		var total = {money: 0, costType: '合计：'};
		$.each(dData.rows ||[], function (i,item) {
			total.money += parseFloat(item.money || '0');
		});
		total.money = total.money.toFixed(2);
		$('#dataGrid').datagrid('reloadFooter',[ total ]);
	}
	
	function deleteRow(index){
		$("#dataGrid").datagrid('deleteRow',index) ;
	}
</script>
</head>
<body>
	<div class="easyui-panel" data-options="fit:true,border:false" style="padding:10px;width:100%;">
		<!-- 替换自动form表单 -->
		<form id="easyuiForm" method="post" action="/console/expense/rewrite/modify"  style="min-width:800px;width:100%;"   >
		 <!--类名属性及默认主键id-->
		 <input type="hidden" name="className" value="com.fs.jzzn.pojo.Expense" />
		 <input type="hidden" name="createrId" value="${SESSION_USER.user.id}" />
		 <input type="hidden" name="childJson" value="[]" >
		 <input type="hidden" name="id" value="" />
		 <input type="hidden" name="department"  />
		 <input type="hidden" name="status" value="" />
		 <input type="hidden" name="processInstanceId" value="" />
		 
		 <div style="margin-bottom:20px">
		  	<input  class="easyui-textbox"  name="expenseNo" style="width:260px;" readonly="readonly" data-options="label:'单据编号:',required:true,prefix:'ZC',timestamp:'yyMMddHHmmss' "/>
		  	<input class="easyui-datebox easyui-validatebox" id="applyDate" name="applyDate" style="width:260px;" data-options="label:'申请日期:',required:true" />
		  	<input class="easyui-textbox easyui-validatebox" name="pm"  value="${SESSION_USER.user.nickName}" style="width:260px;" data-options="label:'申请人:',required:true,validType:'length[0,15]'" />
		 </div>
		 
		  <div style="margin-bottom:20px">
		 		<select class="easyui-combotreegrid" name="orgNo" style="width:260px;" data-options="label:'组织结构:',panelWidth:300,panelWidth:450,required:true,
	           	idField: 'orgNo', 
	           	missingMessage:'必填项',
				treeField: 'orgName',
				sortName:'orgOrder',
				onSelect:function(row){
					$('input[name=department]').val(row.orgName);	    
				},
				sortOrder:'asc',
	            url:'/console/com.fs.base.pojo.Org/list', 
	            queryParams:{isTreeGrid:'true', treeId:'id', treePid:'parentId' },
	            columns:[[    
	                {field:'orgName',			width:190,	title:'组织名称' }
		         	,{field:'orgType',			width:70,	title:'分类' }
		         	,{field:'orgNo',				width:100,	title:'组织编码' }
		         	,{field:'orgLeaderName',	width:70,	title:'负责人'}
	            ]]"></select>
		    	<select class="easyui-combobox" name="contractName" style="width:520px;" data-options="label:'合同名称:',required:true" 
					url="/console/contract/combobox?textField=contractName&valueField=contractName" >
			   </select>
		  	</div>
		  	
		 	<div style="margin-bottom:20px">
		 		<input class="easyui-textbox easyui-validatebox" name="remark" multiline="true" style="width:780px;height:100px;" data-options="label:'备注:',required:true,validType:'length[0,500]'" />
		 	</div>
		 
		 <div style="margin-bottom:20px">
		 		<input class="easyui-layuiupload" name="attachment" style="width:80%;" data-options="label:'附件:',upload:{auto:false,height:250}" />
		 </div>
		 
		 <!-- 支付明细 -->
		 	<div style="margin-bottom:20px;margin-left: 80px;">
				<table id="dataGrid" title="支付明细" style="height:360px;width:80%;"></table>
			</div>
		</form>
	</div>
</body>
</html>